package com.xiegang.servicePrice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiegang.internalcommon.constant.CommonStatusEnum;
import com.xiegang.internalcommon.dto.PriceRule;
import com.xiegang.internalcommon.dto.ResponseResult;
import com.xiegang.servicePrice.mapper.PriceRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author xieGang
 * @since 2023-04-07
 */
@Service
public class PriceRuleService {

    @Autowired
    private PriceRuleMapper priceRuleMapper;

    public ResponseResult add(PriceRule priceRule){

        // 拼接fareType
        String cityCode = priceRule.getCityCode();
        String vehicleType = priceRule.getVehicleType();
        String fareType = cityCode +"$ "+ vehicleType;
        priceRule.setFareType(fareType);

        // 添加版本号
        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code",cityCode);
        queryWrapper.eq("vehicle_type",vehicleType);
        queryWrapper.orderByDesc("fare_version");


        List<PriceRule> priceRules = priceRuleMapper.selectList(queryWrapper);
        Integer fareVersion = 0;
        if(priceRules.size()>0){
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EXISTS.getCode(),CommonStatusEnum.PRICE_RULE_EXISTS.getMessage());
        }

        priceRule.setFareVersion(++fareVersion);


        priceRuleMapper.insert(priceRule);
        return ResponseResult.success();
    }

    public ResponseResult edit(PriceRule priceRule){

        // 拼接fareType
        String cityCode = priceRule.getCityCode();
        String vehicleType = priceRule.getVehicleType();
        String fareType = cityCode +"$ "+ vehicleType;
        priceRule.setFareType(fareType);

        // 添加版本号
        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code",cityCode);
        queryWrapper.eq("vehicle_type",vehicleType);
        queryWrapper.orderByDesc("fare_version");


        List<PriceRule> priceRules = priceRuleMapper.selectList(queryWrapper);
        Integer fareVersion = 0;
        if(priceRules.size()>0){
            PriceRule lasterPriceRule = priceRules.get(0);
//            Double startFare = lasterPriceRule.getStartFare();
//            Integer startMile = lasterPriceRule.getStartMile();
//            Double unitPricePerMile = lasterPriceRule.getUnitPricePerMile();
//            Double unitPricePerMinute = lasterPriceRule.getUnitPricePerMinute();


            fareVersion = lasterPriceRule.getFareVersion();
        }

        priceRule.setFareVersion(++fareVersion);


        priceRuleMapper.insert(priceRule);
        return ResponseResult.success();
    }

    public ResponseResult<Boolean> ifExists(PriceRule priceRule) {

        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code",priceRule.getCityCode());
        queryWrapper.eq("vehicle_type",priceRule.getVehicleType());

        Integer priceRuleNumber = priceRuleMapper.selectCount(queryWrapper);

        if(priceRuleNumber>0){
            return ResponseResult.success(true);
        }
        return ResponseResult.success(false);
    }
}
